package leetcode.median;

import leetcode.base.TreeNode;

import java.util.*;

/**
 * ClassName: $103_zigzagLevelOrder
 * Description:
 * date: 2022-02-12 21:25
 *
 * @author liyifan
 */
public class $103_zigzagLevelOrder {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new LinkedList<>();
        if (root == null)
            return res;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        boolean left = true;
        while (!queue.isEmpty()) {
            int size = queue.size();
            Deque<Integer> path = new LinkedList<>();
            for(int i = 0;i < size;i++) {
                TreeNode treeNode = queue.poll();
                if (left) {
                    path.addLast(treeNode.val);
                } else {
                    path.addFirst(treeNode.val);
                }
                if (treeNode.left != null) {
                    queue.offer(treeNode.left);
                }
                if (treeNode.right != null) {
                    queue.offer(treeNode.right);
                }
            }
            res.add(new LinkedList<>(path));
            left = !left;
        }
        return res;

    }
}
